<?php
/**
 * ClassName: MemberService
 * Description:
 * @Author 周昌
 * @Create 2023/11/15 14:00
 */

namespace service\admin;


use Exception;
use libs\Tool;
use model\ArchivesModel;
use model\MemberInfoModel;
use model\MemberModel;
use service\BaseService;
use think\Db;

class MemberService extends BaseService
{
    protected static $model = 'model\MemberModel';
    public static $sex = [0 => '男', 1 => '女'];

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * #User: Administrator
     * #Time: 2023/11/15 14:14
     * #Author:周昌
     * #Description: 用户性别下拉选择
     * @return array
     */
    public static function memberSexSelect()
    {
        static $member_sex_select = [];
        if (empty($member_sex_select)) {
            foreach (self::$sex as $k => $v) {
                $row = [];
                $row['id'] = $k;
                $row['name'] = $v;
                $member_sex_select[] = $row;
            }
            array_unshift($member_sex_select, ['id' => '', 'name' => '-----请选择-----']);
        }
        return $member_sex_select;
    }

    /**
     * #User: Administrator
     * #Time: 2023/11/16 9:30
     * #Author:周昌
     * #Description: 判断 同一医院下 用户的phone 是否已经存在
     * @param $where
     * @return array
     */
    public static function memberIsExist($id = null, $phone)
    {
        $map1 = [
            ['phone', '=', $phone],
        ];
        if ($id) {
            $map1[] = ['id', '<>', $id];
        }
        $res = self::$model::where($map1)->find();
        if ($res) {
            return Tool::Error('手机号已存在', $res);
        }
        return Tool::Success('成功');
    }

    //新增用户+新增member_info(家庭组)
    public static function addMemberAndInfo($data)
    {
        Db::startTrans();
        try {
            $member_id = self::$model::insertInfo($data);
            if (!$member_id) {
                Db::rollback();
                return Tool::Error('添加会员失败');
            }
            $insert = [
                'member_id' => $member_id,
                'name' => $data['real_name'],
                'sex' => $data['sex'],
//                'phone' => $data['phone'],
                'addtime' => $data['addtime'],
            ];
            $member_info_id = MemberInfoModel::insertInfo($insert);
            if (!$member_info_id) {
                Db::rollback();
                return Tool::Error('添加会员信息失败');
            }
            Db::commit();
            return Tool::Success('添加成功',['member_id'=>$member_id]);
        } catch (Exception $exception) {
            Db::rollback();
            return Tool::Error($exception->getMessage());
        }
    }

    /**
     * #User: Administrator
     * #Time: 2023/12/6 10:35
     * #Author:周昌
     * #Description:根据注册用户手机号关联查询member_info(家庭组)
     * @param $phone
     * @param string $member_field
     * @param string $info_field
     * @return array
     */
    public static function getInfoByPhone($phone, $member_field = '*', $info_field = '*')
    {
        $where = [
            'phone' => $phone
        ];
        $res = self::$model::field($member_field)->where($where)->with(['info' => function ($query) use ($info_field) {
            $query->field($info_field)->select();
        }])->find();
        return $res ? $res->toArray() : [];
    }

    //获取家庭组
    public static function getFamilyGroup($where)
    {
        $member_info = MemberModel::getInfo($where, 'id,real_name,phone');
        if ($member_info) {
            $member_info['family_group'] = $member_info['real_name'] . '-' . $member_info['phone'];
            return Tool::Success('成功', $member_info);
        } else {
            return Tool::Error('该手机号尚未注册请先去注册');
        }
    }
    //批量获取家庭组
    public static function getMoreFamilyGroup($where)
    {
        $member_info = self::$model::getListColumn($where, 'id,real_name,phone');
        if ($member_info) {
            foreach ($member_info as &$v){
                $v['family_group']=$v['real_name'] . '-' . $v['phone'];
            }
        }
        return $member_info;
    }

    //列表数据
    public static function getAjaxData($where)
    {
        $table = self::$model::order('id desc');
        return self::tableJsonData($table, $where, function ($data) {
            $data['sex_name'] = self::$sex[$data['sex']];
            $data['hospital_name'] = $data['hospital_id'] ? HospitalService::getNameByKey($data['hospital_id'], '') : '';
            return $data;
        });
    }

}